package com.batch.job;

import com.batch.decider.StatusDecider1;
import com.batch.incrementer.MyTimeStampParamIncrementer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;


// 主要学习熟悉 决策器 的概念(学习章节5.1)
// 学习当前Job，请将@SpringBootApplication 取消注释，并将其他Job的@SpringBootApplication 方法注释,否则会对当前结果产生干扰

/**
 *
 *
 *
 *
 */


//@SpringBootApplication
// 因为用的Step路径在别的目录下，因此需要手动加ComponentScan
@ComponentScan(basePackages = "com.batch",
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.batch.job"))
// EnableBatchProcessing 使spring创建springbatch操作相关类对象
@EnableBatchProcessing
public class ControlJob1 {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    // Job构建工厂，用于构建Job对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    private int timer = 9;

    @Bean
    public Tasklet controlTasklet1_01() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            System.out.println("controlTasklet1_01");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet controlTasklet1_02() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            System.out.println("controlTasklet1_02");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet controlTasklet1_03() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            System.out.println("controlTasklet1_03");
            return RepeatStatus.FINISHED;
        };
    }


    @Bean
    public Step controlStep1_01() {
        return stepBuilderFactory.get("control-step1-01")
                .tasklet(controlTasklet1_01()).build();
    }

    @Bean
    public Step controlStep1_02() {
        return stepBuilderFactory.get("control-step1-02")
                .tasklet(controlTasklet1_02()).build();
    }

    @Bean
    public Step controlStep1_03() {
        return stepBuilderFactory.get("control-step1-03")
                .tasklet(controlTasklet1_03()).build();
    }

    @Bean
    public JobExecutionDecider myDecider(){
        return new StatusDecider1();
    }

    @Bean
    public Job controlJob1_01() {
        return jobBuilderFactory.get("control-job1-01")
                .start(controlStep1_01())
                .next(myDecider())
                .from(myDecider()).on("A").to(controlStep1_02())
                .from(myDecider()).on("*").to(controlStep1_03())
                .end()
                .incrementer(new MyTimeStampParamIncrementer())
                .build();
    }



    public static void main(String[] args) {
        SpringApplication.run(ControlJob1.class, args);
    }


}
